<?php

namespace App\Game;

use App\Models\Def\MainGamePlat;
use App\Models\QuotaHandicap;
use Carbon\Carbon;

class qly extends Game
{
    private $platName = 'qly';
    const API_PLAYER_CREATE = '/agent/login';
    const API_PLAYER_BALANCE= '/agent/getbalance';
    const API_TRANSFER_IN   = '/agent/payup';
    const API_TRANSFER_OUT  = '/agent/paydown';
    const API_JOINGAME      = '/agent/login';
    const API_TRANSFER_CHECK= '/agent/getorderstatus';
    const API_RESULT        = '/record/loadrecords';
    const API_KICK          = '/agent/kickout';


    public function __construct()
    {

        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }

     public function paramEncode($param)
    {
        $iv = substr($this->apiKey,0,16);
        $method="AES-256-CBC";
        $data = json_encode($param);
        $s1 = openssl_encrypt($data, $method, $this->apiKey , true, $iv);
        return base64_encode($s1);
    }

    public function paramDecode($param_str)
    {
        $iv = substr($this->apiKey,0,16);
        $method = "AES-256-CBC";
        $data = base64_decode($param_str);
        $s1 = openssl_decrypt($data, $method ,$this->apiKey , true, $iv); 
        return json_decode($s1);
    }

    public function makeSign($timestamp)
    {
        $sign_str = "".$this->agent.$timestamp.$this->apiKey;
        return strtoupper(md5($sign_str));
    }

    public function combineURL($baseURL,$agv)
    {
        $combined = $baseURL."?";
        $valueArr = array();
        foreach($agv as $key => $val){
            $valueArr[] = "$key=$val";
        }
        $keyStr = implode("&",$valueArr);
        $combined .= ($keyStr);
        return $combined;
    }

    //创建用户
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $url = $this->gameUrl . self::API_PLAYER_CREATE;
        $timestamp=time();

        $params_raw = array(
            "acc" => $accountUserName
        );
        $params =$this->paramEncode($params_raw);
        $sign = $this->makeSign($timestamp);
        $rqagv = array (
            "agent" => $this->agent,
            "timestamp" => $timestamp,
            "params" => URLEncode($params),
            "sign" => $sign
        );

        $url = $this->combineURL($url,$rqagv);
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);
         if (isset($outputarr['code']) && $outputarr['code'] == 0 && $outputarr['d']['isNew']) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败'];
        }
    }

    //获取余额
    public function curlGetBalance($accountUserName)
    {
        $url = $this->gameUrl . self::API_PLAYER_BALANCE;
        $timestamp=time();
        $params_raw = array(
            "acc" => $accountUserName
        );

        $params =$this->paramEncode($params_raw);
        $sign = $this->makeSign($timestamp);
        $rqagv = array (
            "agent" => $this->agent,
            "timestamp" => $timestamp,
            "params" => URLEncode($params),
            "sign" => $sign
        );

        $url = $this->combineURL($url,$rqagv);
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['code']) && $outputarr['code'] == 0) {
            return ['success' => true, 'data' => ['balance' => $outputarr['d']['balance']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '查询失败'];
        }
    }

    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $url = $this->gameUrl . self::API_TRANSFER_IN;
        $timestamp=time();
        $params_raw = array(
            "acc" => $accountUserName,
            "orderid"=>$transferid,
            "money"=>number_format($price,2,'.','')
        );
        $params =$this->paramEncode($params_raw);
        $sign = $this->makeSign($timestamp);
        $rqagv = array (
            "agent" => $this->agent,
            "timestamp" => $timestamp,
            "params" => URLEncode($params),
            "sign" => $sign
        );
        $url = $this->combineURL($url,$rqagv);
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['code'])) {
            if ($outputarr['code'] == 0) {
                return ['success' => true, 'data' => ['transferid' => $outputarr['d']['orderid'], 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $url = $this->gameUrl . self::API_TRANSFER_OUT;
        $timestamp=time();
        $params_raw = array(
            "acc" => $accountUserName,
            "orderid"=>$transferid,
            "money"=>number_format($price,2,'.','')
        );

        $params =$this->paramEncode($params_raw);
        $sign = $this->makeSign($timestamp);
        $rqagv = array (
            "agent" => $this->agent,
            "timestamp" => $timestamp,
            "params" => URLEncode($params),
            "sign" => $sign
        );

        $url = $this->combineURL($url,$rqagv);
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['code'])) {
            if ($outputarr['code'] == 0) {
                return ['success' => true, 'data' => ['transferid' => $outputarr['d']['orderid'], 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $url = $this->gameUrl . self::API_TRANSFER_CHECK;
        $timestamp=time();
        $params_raw = array(
            "acc" => $request->input('accountUserName'),
            "orderid"=>$request->input('transferid')
        );
        $params =$this->paramEncode($params_raw);
        $sign = $this->makeSign($timestamp);
        $rqagv = array (
            "agent" => $this->agent,
            "timestamp" => $timestamp,
            "params" => URLEncode($params),
            "sign" => $sign
        );

        $url = $this->combineURL($url,$rqagv);
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

         if (isset($outputarr['code'])) 
         {
            if ($outputarr['code'] == 0&&$outputarr['d']['status']==3) {
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '检查转账失败'];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //踢线

    public function curlKick($request)
    {
        $url = $this->gameUrl . self::API_KICK;
        $timestamp=time();
        $params_raw = array(
            "acc" => $request->input('accountUserName')
        );
        $params =$this->paramEncode($params_raw);
        $sign = $this->makeSign($timestamp);
        $rqagv = array (
            "agent" => $this->agent,
            "timestamp" => $timestamp,
            "params" => URLEncode($params),
            "sign" => $sign
        );

        $url = $this->combineURL($url,$rqagv);
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);
        if(isset($outputarr['code'])&&$outputarr['code'] == 0)
        {
             return ['success' => true, 'data' => [], 'message' => '成功踢线'];
        }
        else
        {
             return ['success' => false, 'data' => [], 'message' => '此平台没有踢线此功能'];
        }
    }

    //进入游戏
    public function curlJoinGame($request)
    {

        $url = $this->gameUrl . self::API_JOINGAME;
        $timestamp=time();

        $params_raw = array(
            "acc" => $request->input('accountUserName'),
            "game"=> $request->input('gamecode')
        );
        $params =$this->paramEncode($params_raw);
        $sign = $this->makeSign($timestamp);
        $rqagv = array (
            "agent" => $this->agent,
            "timestamp" => $timestamp,
            "params" => URLEncode($params),
            "sign" => $sign
        );

        $url = $this->combineURL($url,$rqagv);
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['code']) && $outputarr['code'] == 0) {
            return ['success' => true, 'data' => ['url' => $outputarr['d']['url'] , 'method' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }

    //进入游戏
    public function curlJoinMobileGame($request)
    {
        return $this->curlJoinGame($request);
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        return [];
        $page = 1;
        $pagesize = 500;
        $timestamp=time();
        $starttime = $startTime->timestamp;
        $endtime = $endTime->timestamp;
        $url = $this->gameUrl . self::API_RESULT;
        $data = [];
        
        do {
            $flag = false;
            $params_raw = array(
                "expids"=>[1,2],
                "page"=>$page,
                "pagenum"=>$pagesize,
                "stime" => $starttime,
                "etime"=>  $endtime
            );

            $params =$this->paramEncode($params_raw);
            $sign = $this->makeSign($timestamp);

           $rqagv = array (
            "agent" => $this->agent,
            "timestamp" => $timestamp,
            "params" => URLEncode($params),
            "sign" => $sign
        );

        $url = $this->combineURL($url,$rqagv);
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);
            if (isset($outputarr['code'])) {
                if ($outputarr['code'] == 0) 
                {
                    foreach ($outputarr['d']['data'] as $key => $value) 
                    {
                        $arr=array();
                        $arr['sessionid']=$value['sessionid'];
                        $arr['acc']=$value['acc'];
                        $arr['realput']=bcdiv($value['realput'],10000,2);
                        $arr['allput']=bcdiv($value['allput'],10000,2);
                        $arr['chg']=bcdiv($value['chg'],10000,2);
                        $arr['kind']=$value['kind'];
                        $arr['bettime']=$value['stime'];
                        $data[]=$arr;
                    }
                    $maxpagesize=$pagesize*$page;
                   
                   if( $maxpagesize<$outputarr['d']['total'])
                   {
                      $flag = true;
                   }
                }
            }
            $page++;
        } while ($flag);
        return $data;
    }
}